package xg2013.totoro.web;



import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import xg2013.totoro.domain.User;
import xg2013.totoro.service.ActorService;
import xg2013.totoro.service.GetPresentUser;
import xg2013.totoro.service.RoleService;
import xg2013.totoro.service.TestTotoroService;
import xg2013.totoro.service.UserRoleService;
import xg2013.totoro.service.UserService;

@Controller
public class LoginController {
	@Autowired
	private TestTotoroService testTotoroService;
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private UserService userService;
	@Autowired
	private ActorService actorService;
	@Autowired
	private RoleService roleService;
	@RequestMapping(value = "/login.html",method = RequestMethod.GET)
	public String login(Model model){
		model.addAttribute("error", "");
		return "login/login";
	}
	
	@RequestMapping(value = "/login.html",method = RequestMethod.POST)
	public String index(User user,Model model,HttpSession session){
		if(userService.uniqueResult("username", user.getUsername())!=null
				&& userService.uniqueResult("username", user.getUsername()).getPassword().equals(user.getPassword())){
			GetPresentUser.presentUserId = userService.uniqueResult("username", user.getUsername()).getId();
			
			//System.out.println(GetPresentUser.user.getId());
			session.setAttribute("user", user);
			return "login/index";
		}else{
			model.addAttribute("error","用户名或密码错误");
			return "login/login";
		}
		
	}
	
	@RequestMapping(value = "/admin_top.html",method = RequestMethod.GET)
	public String toTop(Model model){
		
			User user = userService.get(GetPresentUser.presentUserId);
			model.addAttribute("user", user);
			//System.out.println(GetPresentUser.user.getId());
			return "login/admin_top";
		
	}
	@RequestMapping(value = "/left.html",method = RequestMethod.GET)
	public String toLeft(Model model){
		User user = userService.get(GetPresentUser.presentUserId);
		model.addAttribute("user", user);
			//System.out.println(GetPresentUser.user.getId());
			return "login/left";
		
	}
}
